package com.wefans.wefansapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetImage;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;

public class VersionInfoActivity extends BaseActivity implements IOnNetworkListener{

	private DrawerLayout   m_DrawerLayout  = null;
    private FrameLayout    m_DrawerView    = null;
    private TextView	   m_CurrentVer	   = null;
    private TextView	   m_LatestVer	   = null;
    private TextView	   m_InfoRule	   = null;
    private ImageView	   m_GoRuleBtn	   = null;
//	private MFNetworkManager m_oNetworManager = null;
    private MFDataManager    m_oDataManager   = null;
    private Intent it;
//    private Toast toastView;
	
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_versioninfo);
		initialize();
		installEvent();
		
	}
	
	@Override
    protected void onPause()
    {
        MFDebug.d(">> onPause()");
        
        notifyDoClaseDrawerMenu();
        
        super.onPause();
    }

    @Override
    protected void notifyDoClaseDrawerMenu()
    {
        MFDebug.d(">> notifyDoClaseDrawerMenu()");
        
        super.notifyDoClaseDrawerMenu();
        
        if( m_DrawerLayout == null ) {
            MFDebug.e(new Exception("There are NO Drawer-Layout"));
            return;
        }
        
        if( m_DrawerView == null ) {
            MFDebug.e(new Exception("There are NO Drawer-View"));
            return;
        }
        
        m_DrawerLayout.closeDrawer(m_DrawerView);
        m_sdQuickMenu.close();
    }
	
	@Override
    public void onClick(View v)
    {
        MFDebug.d(">> onClick()");
        
        switch( v.getId() ) {
            case R.id.IB_VERSION_INFO_HEADER_LEFT_BUTTON :
                m_DrawerLayout.openDrawer(m_DrawerView);
                break;
            case R.id.IB_VERSION_INFO_HEADER_RIGHT_BUTTON :
            	MFDebug.d("++ v.getId() : " + "IB_BENEFIT_HEADER_RIGHT_BUTTON");
				it = new Intent(getApplicationContext(), MainActivity.class);
				startActivityForResult(it,v.getId());
                break;
            case R.id.IV_VERSION_INFO_BTN :
            	MFDebug.d("++ v.getId() : " + "IB_BENEFIT_HEADER_RIGHT_BUTTON");
				it = new Intent(getApplicationContext(), TermsActivity.class);
				startActivityForResult(it,v.getId());
                break;
            default :
                break;
        }
        
        super.onClick(v);
    }
	
    private void initialize()
    {
    	 MFDebug.d(">> initialize()");
    	 
//    	 m_oNetworManager = MFNetworkManager.getInstance(this);
         m_oDataManager   = MFDataManager.getInstance(this);
         
         //Contents layout adding
         FrameLayout containerContents = (FrameLayout) findViewById(R.id.FL_VERSION_INFO_CONTAINER);
         addContentsLayout(containerContents, R.layout.layout_versioninfo);
         addQuickMenuLayout(containerContents);
         
         //Drawer menu adding
         m_DrawerView = (FrameLayout) findViewById(R.id.FL_VERSION_INFO_DRAWER_LAYOUT);
         addDrawerLayout(m_DrawerView);
         
         //set drawer layout (root)
         m_DrawerLayout = (DrawerLayout) findViewById(R.id.DL_VERSION_INFO_NAVIGATION_LAYOUT);
         
         //todo Get Latest Version Info ....
    }
    
    private void installEvent()
    {
    	((ImageButton) findViewById(R.id.IB_VERSION_INFO_HEADER_LEFT_BUTTON)).setOnClickListener(this);
    	((ImageButton) findViewById(R.id.IB_VERSION_INFO_HEADER_RIGHT_BUTTON)).setOnClickListener(this);
    	m_CurrentVer = (TextView)findViewById(R.id.TV_CURRENT_VERSION);
    	m_LatestVer = (TextView)findViewById(R.id.TV_LATEST_VERSION);
    	m_InfoRule = (TextView)findViewById(R.id.TV_LATEST_RULE);
    	m_GoRuleBtn = (ImageView)findViewById(R.id.IV_VERSION_INFO_BTN);
    	
    	String strAppVer = "";
    	if (m_oDataManager.getVersionInfo() != null && m_oDataManager.getVersionInfo().getApp_ver() != null) {
    		strAppVer = m_oDataManager.getVersionInfo().getApp_ver();
    	}
    	m_CurrentVer.setText(this.getString(R.string.version_info_current) +"("+ strAppVer +")");
    	m_LatestVer.setText(this.getString(R.string.version_info_latest) +"("+ "-----"  +")");
    	m_InfoRule.setText(R.string.version_info_rule);
    	m_GoRuleBtn.setOnClickListener(this);
        
    }

    @Override
	public void onNetMessage(int resultCode, NetMessage message) {
	    MFDebug.d(">> onNetMessage()");
	    MFDebug.d("++ resultCode : " + resultCode);
	    MFDebug.d("++ message : " + message);
	    
	    try {
	        //서버 여동 데이터
	        if( message instanceof NetJsonData ) {
	            long magicNum = ((NetJsonData) message).getMagicNum();
	            String json = ((NetJsonData) message).getResponseJson();
	            
	            if( json == null ) {
	                throw new Exception("invalid json data");
	            }
	            
	            Gson gson = new Gson();
	           //todo Get Version Info 
//	            if( magicNum == MFNetworkManager.REQUEST_BENEFIT_VOTE ) {
//                	mBenefitVoteDate = (BenefitVoteData)gson.fromJson(json, BenefitVoteData.class);
//                    if( mBenefitVoteDate == null ) {
//                        throw new Exception("Gson parsing error");
//                    }
//                    if( mBenefitVoteDate.getResultcode().equals("1") ){
//                    	toastVote = Toast.makeText(this, getString(R.string.benefit_vote_success), Toast.LENGTH_LONG);
//                    }else{
//                    	toastVote = Toast.makeText(this, mBenefitVoteDate.getErrorcode() + ":" + getString(R.string.benefit_vote_fail), Toast.LENGTH_LONG);
//                    }
//                    toastVote.show();
                }
//	        }
	        //이미지 데이터
	        else if( message instanceof NetImage ) {
	            
	        }
	        else {
	            //ignore...
	        }
	    } catch(Exception e) {
	        MFDebug.e(e);
	    }
	}
}